// 地址相关工具函数

/**
 * 处理默认地址逻辑
 * 确保只有一个地址为默认地址
 * @param {Array} addressList - 地址列表
 * @param {string} newDefaultId - 新的默认地址ID
 * @returns {Array} 更新后的地址列表
 */
export const handleDefaultAddress = (addressList, newDefaultId) => {
  return addressList.map(address => ({
    ...address,
    isDefault: address.id === newDefaultId
  }));
};

/**
 * 获取默认地址
 * @param {Array} addressList - 地址列表
 * @returns {Object|null} 默认地址对象，如果没有则返回null
 */
export const getDefaultAddress = (addressList) => {
  return addressList.find(address => address.isDefault) || null;
};

/**
 * 检查是否有默认地址
 * @param {Array} addressList - 地址列表
 * @returns {boolean} 是否有默认地址
 */
export const hasDefaultAddress = (addressList) => {
  return addressList.some(address => address.isDefault);
};

/**
 * 格式化地址显示
 * @param {Object} address - 地址对象
 * @returns {string} 格式化后的地址字符串
 */
export const formatAddress = (address) => {
  if (!address) return '';
  
  const region = address.address?.region?.label || '';
  const detailed = address.address?.detailed || '';
  
  return `${region} ${detailed}`.trim();
};

/**
 * 验证地址数据
 * @param {Object} addressData - 地址数据
 * @returns {Object} 验证结果 {valid: boolean, message: string}
 */
export const validateAddress = (addressData) => {
  if (!addressData.name || addressData.name.trim() === '') {
    return { valid: false, message: '请输入收件人姓名' };
  }
  
  if (!addressData.phone || addressData.phone.trim() === '') {
    return { valid: false, message: '请输入收件人电话号码' };
  }
  
  if (!addressData.address || addressData.address.trim() === '') {
    return { valid: false, message: '请输入收件人详细地址' };
  }
  
  return { valid: true, message: '' };
}; 